﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class EnterBH : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    static string[] nguyenNhan;
    static string[] tiLe;
    static string[] idNguyenNhan;
    static string[] khacPhuc;
    static int numberOfRow;
    static int presentNumber = 1;

    protected void buttonTimLoi_Click(object sender, EventArgs e)
    {
        presentNumber = 1;
        string tc = tbTrieuChung.Text;
        string sql = "SELECT rule_nguyennhan.ID, rule_nguyennhan.CF as CF, rule_nguyennhan.`Name` FROM rule_nguyennhan, trieuchung WHERE rule_nguyennhan.IDTrieuChung = trieuchung.ID AND trieuchung.`Name` = '" + tc + "' GROUP BY CF DESC, ID";
        DataTable dt = LoadDataFromDataBase(sql);
        numberOfRow = dt.Rows.Count;
        if (numberOfRow > 0)
        {
            nguyenNhan = new string[numberOfRow];
            tiLe = new string[numberOfRow];
            idNguyenNhan = new string[numberOfRow];
            khacPhuc = new string[numberOfRow];
            for (int i = 0; i < numberOfRow; i++)
            {
                nguyenNhan[i] = dt.Rows[i]["Name"].ToString();
                tiLe[i] = " (Tỉ lệ: " + dt.Rows[i]["CF"].ToString() + ")";
                idNguyenNhan[i] = dt.Rows[i]["ID"].ToString();
                string sql2 = "SELECT KhacPhuc FROM khacphuc WHERE IDNguyenNhan = '" + idNguyenNhan[i] + "'";
                DataTable dt2 = LoadDataFromDataBase(sql2);
                //int numberOfRows2 = dt.Rows.Count;
                khacPhuc[i] = dt2.Rows[0]["KhacPhuc"].ToString();
            }

            labelNguyenNhan.Text = nguyenNhan[0] + tiLe[0];
            labelKhacPhuc.Text = khacPhuc[0];
        }
        else
        {
            labelNguyenNhan.Text = "null";
            labelKhacPhuc.Text = "null";
        }

    }

    protected void buttonLoiKhac_Click(object sender, EventArgs e)
    {
        if (numberOfRow == 1)
        {
            labelNguyenNhan.Text = "null";
            labelKhacPhuc.Text = "null";
        }
        else
        {
            if (presentNumber < numberOfRow)
            {
                labelNguyenNhan.Text = nguyenNhan[presentNumber] + tiLe[presentNumber];
                labelKhacPhuc.Text = khacPhuc[presentNumber];
                presentNumber++;
            }
            else
            {
                labelNguyenNhan.Text = "null";
                labelKhacPhuc.Text = "null";
            }
        }
    }

    public DataTable LoadDataFromDataBase(string sql)
    {
        class_database db = new class_database();
        DataTable dt = new DataTable();
        dt = db.GetDataTable(sql);
        return dt;
    }
}